# Copyright (C) 2007 Laszlo Pandy <laszlok2@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import gtk
import gobject
import MoneyOwedView
import SummaryView
import PaymentView
import CategoryView
import PeopleView

class ViewSwitcherVBox(gtk.VBox):
	
	__gsignals__ = {
		"view-changed" : ( gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, () )
	}
		
	VIEW_STRINGS = ("owed", "summary", "payment", "category", "people")
	
	def __init__(self, window, account):
		gtk.VBox.__init__(self)
		
		self.set_spacing(6)
		self.set_border_width(6)
		
		self.viewButtonBox = gtk.HButtonBox()
		self.viewButtonBox.set_layout(gtk.BUTTONBOX_SPREAD)
		self.viewButtonBox.set_spacing(12)
		self.pack_start(self.viewButtonBox, False, False)
		
		account.connect("loaded", self.OnAccountLoaded)
		
		self.owedButton = gtk.ToggleButton("Money Owed")
		self.summaryButton = gtk.ToggleButton("Summary")
		self.paymentButton = gtk.ToggleButton("Payments")
		self.categoryButton = gtk.ToggleButton("Categories")
		self.peopleButton = gtk.ToggleButton("People")
		
		self.buttonList = (self.owedButton, self.summaryButton,
				self.paymentButton, self.categoryButton,
				self.peopleButton)
		
		for button in self.buttonList:
			button.connect("clicked", self.OnSwitchViewButton)
			self.viewButtonBox.add(button)
			
		self.owedView = MoneyOwedView.MoneyOwedView(account)
		self.summaryView = SummaryView.SummaryView(account)
		self.paymentView = PaymentView.PaymentView(account, window)
		self.categoryView = CategoryView.CategoryView(account, window)
		self.peopleView = PeopleView.PeopleView(account)
		
		self.viewList = [self.owedView, self.summaryView, 
			self.paymentView, self.categoryView,
			self.peopleView]
			
		self.viewDict = dict(zip(self.buttonList, self.viewList))	
		
		self.currentView = None
		self.settingButtons = False
		self.OnOwedView()
	
	def OnAccountLoaded(self, account=None):
		if self.currentView:
			self.currentView.UpdateInterface()
		for i in self.viewList:
			if i is not self.currentView:
				i.UpdateInterface()
	
	def OnSwitchViewButton(self, button):
		if not self.settingButtons:
			# if they try and untoggle the currently selected view
			# retoggle it
			if not button.get_active():
				button.set_active(True)
			view = self.viewDict[button]
			self.SwitchView(view)
		
	def SwitchView(self, view):
		if self.currentView == view:
			return
		if self.currentView:
			self.remove(self.currentView)
		
		self.pack_end(view)
		self.currentView = view
		
		self.settingButtons = True
		for button, view in self.viewDict.iteritems():
			button.set_active(view == self.currentView)
		self.settingButtons = False
		
		self.emit("view-changed")
		
	def GetCurrentViewString(self):
		if self.currentView:
			index = self.viewList.index(self.currentView)
			return self.VIEW_STRINGS[index]
		
	def OnOwedView(self, widget=None):
		self.SwitchView(self.owedView)
	
	def OnSummaryView(self, widget=None):
		self.SwitchView(self.summaryView)
	
	def OnPaymentView(self, widget=None):
		self.SwitchView(self.paymentView)
	
	def OnCategoryView(self, widget=None):
		self.SwitchView(self.categoryView)
	
	def OnPeopleView(self, widget=None):
		self.SwitchView(self.peopleView)
		